

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>agpy.densitymap &mdash; agpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/extra.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1.2 documentation" href="../../index.html" />
    <link rel="up" title="agpy" href="../agpy.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
    <link rel="stylesheet" type="text/css" href="../../_static/extra.css" />
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1.2 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for agpy.densitymap</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Build a density map out of a list of coordinates</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">pyfits</span>
    <span class="kn">import</span> <span class="nn">pyregion</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&quot;densitymap requires pyfits and pyregion&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">from</span> <span class="nn">agpy</span> <span class="kn">import</span> <span class="n">gaussfitter</span>

<div class="viewcode-block" id="dmregion"><a class="viewcode-back" href="../../agpy.html#agpy.densitymap.dmregion">[docs]</a><span class="k">def</span> <span class="nf">dmregion</span><span class="p">(</span><span class="n">header</span><span class="p">,</span><span class="n">region</span><span class="p">,</span><span class="n">outfits</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">smoothpix</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span><span class="n">clobber</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a valid .FITS header with WCS coordinates</span>
<span class="sd">    and a ds9 region file, creates a density map of </span>
<span class="sd">    ds9 objects (doesn&#39;t filter by shape type).</span>
<span class="sd">    Default smoothing is by a sigma=2 pixels gaussian.</span>
<span class="sd">    Specify outfits to write to a fits file, otherwise</span>
<span class="sd">    returns the map</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">reg_to_xy</span><span class="p">(</span><span class="n">region</span><span class="p">,</span><span class="n">header</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">densitymap</span><span class="p">(</span><span class="n">header</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">outfits</span><span class="o">=</span><span class="n">outfits</span><span class="p">,</span><span class="n">smoothpix</span><span class="o">=</span><span class="n">smoothpix</span><span class="p">,</span><span class="n">clobber</span><span class="o">=</span><span class="n">clobber</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="reg_to_xy"><a class="viewcode-back" href="../../agpy.html#agpy.densitymap.reg_to_xy">[docs]</a><span class="k">def</span> <span class="nf">reg_to_xy</span><span class="p">(</span><span class="n">region</span><span class="p">,</span><span class="n">header</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">region</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">:]</span> <span class="o">==</span> <span class="s">&#39;reg&#39;</span><span class="p">:</span>
        <span class="n">regfile</span> <span class="o">=</span> <span class="n">pyregion</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">region</span><span class="p">)</span>
        <span class="n">pix_reg</span> <span class="o">=</span> <span class="n">regfile</span><span class="o">.</span><span class="n">as_imagecoord</span><span class="p">(</span><span class="n">header</span><span class="p">)</span>
        <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">transpose</span><span class="p">([</span><span class="n">rim</span><span class="o">.</span><span class="n">coord_list</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="k">for</span> <span class="n">rim</span> <span class="ow">in</span> <span class="n">pix_reg</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;don&#39;t do this it&#39;s tough.&quot;</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        regfile = readcol(region,asdict=True)</span>
<span class="sd">        if regfile.has_key(&#39;ra&#39;):</span>
<span class="sd">            ra = regfile[&#39;ra&#39;]</span>
<span class="sd">            dec = regfile[&#39;dec&#39;]</span>
<span class="sd">            coordsys = &#39;radec&#39;</span>
<span class="sd">        elif regfile.has_key(&#39;&#39;)</span>
<span class="sd">        wcs = pywcs.WCS(header=header)</span>
<span class="sd">        &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span>

</div>
<div class="viewcode-block" id="densitymap"><a class="viewcode-back" href="../../agpy.html#agpy.densitymap.densitymap">[docs]</a><span class="k">def</span> <span class="nf">densitymap</span><span class="p">(</span><span class="n">header</span><span class="p">,</span><span class="n">xi</span><span class="p">,</span><span class="n">yi</span><span class="p">,</span><span class="n">smoothpix</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">outfits</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span><span class="n">clobber</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates a source-density map given a region file</span>
<span class="sd">    or a list of coordinates</span>

<span class="sd">    this should be done with np.histogram2d</span>
<span class="sd">    &quot;&quot;&quot;</span>


    <span class="n">ny</span><span class="p">,</span><span class="n">nx</span> <span class="o">=</span> <span class="n">header</span><span class="p">[</span><span class="s">&#39;NAXIS2&#39;</span><span class="p">],</span><span class="n">header</span><span class="p">[</span><span class="s">&#39;NAXIS1&#39;</span><span class="p">]</span>
    <span class="n">blankim</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">ny</span><span class="p">,</span><span class="n">nx</span><span class="p">))</span>

    <span class="n">xf</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">xi</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;int&#39;</span><span class="p">)</span>
    <span class="n">xc</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">xi</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;int&#39;</span><span class="p">)</span>
    <span class="n">yf</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">yi</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;int&#39;</span><span class="p">)</span>
    <span class="n">yc</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">yi</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s">&#39;int&#39;</span><span class="p">)</span>
    <span class="n">weight1</span> <span class="o">=</span> <span class="p">(</span><span class="n">xi</span><span class="o">-</span><span class="n">xf</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yi</span><span class="o">-</span><span class="n">yf</span><span class="p">)</span>
    <span class="n">weight2</span> <span class="o">=</span> <span class="p">(</span><span class="n">xi</span><span class="o">-</span><span class="n">xf</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yc</span><span class="o">-</span><span class="n">yi</span><span class="p">)</span>
    <span class="n">weight3</span> <span class="o">=</span> <span class="p">(</span><span class="n">xc</span><span class="o">-</span><span class="n">xi</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yi</span><span class="o">-</span><span class="n">yf</span><span class="p">)</span>
    <span class="n">weight4</span> <span class="o">=</span> <span class="p">(</span><span class="n">xc</span><span class="o">-</span><span class="n">xi</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yc</span><span class="o">-</span><span class="n">yi</span><span class="p">)</span>
    <span class="n">OK1</span> <span class="o">=</span> <span class="p">(</span><span class="n">xf</span> <span class="o">&lt;</span> <span class="n">nx</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yf</span> <span class="o">&lt;</span> <span class="n">ny</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">OK2</span> <span class="o">=</span> <span class="p">(</span><span class="n">xf</span> <span class="o">&lt;</span> <span class="n">nx</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yc</span> <span class="o">&lt;</span> <span class="n">ny</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">OK3</span> <span class="o">=</span> <span class="p">(</span><span class="n">xc</span> <span class="o">&lt;</span> <span class="n">nx</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yf</span> <span class="o">&lt;</span> <span class="n">ny</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">OK4</span> <span class="o">=</span> <span class="p">(</span><span class="n">xc</span> <span class="o">&lt;</span> <span class="n">nx</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">yc</span> <span class="o">&lt;</span> <span class="n">ny</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">blankim</span><span class="p">[</span><span class="n">yc</span><span class="p">[</span><span class="n">OK4</span><span class="p">],</span><span class="n">xc</span><span class="p">[</span><span class="n">OK4</span><span class="p">]]</span> <span class="o">+=</span> <span class="n">weight4</span><span class="p">[</span><span class="n">OK4</span><span class="p">]</span>
    <span class="n">blankim</span><span class="p">[</span><span class="n">yc</span><span class="p">[</span><span class="n">OK3</span><span class="p">],</span><span class="n">xf</span><span class="p">[</span><span class="n">OK3</span><span class="p">]]</span> <span class="o">+=</span> <span class="n">weight3</span><span class="p">[</span><span class="n">OK3</span><span class="p">]</span>
    <span class="n">blankim</span><span class="p">[</span><span class="n">yf</span><span class="p">[</span><span class="n">OK2</span><span class="p">],</span><span class="n">xc</span><span class="p">[</span><span class="n">OK2</span><span class="p">]]</span> <span class="o">+=</span> <span class="n">weight2</span><span class="p">[</span><span class="n">OK2</span><span class="p">]</span>
    <span class="n">blankim</span><span class="p">[</span><span class="n">yf</span><span class="p">[</span><span class="n">OK1</span><span class="p">],</span><span class="n">xf</span><span class="p">[</span><span class="n">OK1</span><span class="p">]]</span> <span class="o">+=</span> <span class="n">weight1</span><span class="p">[</span><span class="n">OK1</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">smoothpix</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">xax</span><span class="p">,</span><span class="n">yax</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">indices</span><span class="p">(</span><span class="n">blankim</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="n">kernel</span> <span class="o">=</span> <span class="n">gaussfitter</span><span class="o">.</span><span class="n">twodgaussian</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="n">nx</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="n">ny</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="n">smoothpix</span><span class="p">],</span><span class="n">circle</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">rotate</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">vheight</span><span class="o">=</span><span class="mi">0</span><span class="p">)(</span><span class="n">xax</span><span class="p">,</span><span class="n">yax</span><span class="p">)</span>
        <span class="n">kernelfft</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">fft2</span><span class="p">(</span><span class="n">kernel</span><span class="p">)</span>
        <span class="n">imfft</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">fft2</span><span class="p">(</span><span class="n">blankim</span><span class="p">)</span>
        <span class="n">dm</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">fftshift</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">ifft2</span><span class="p">(</span><span class="n">kernelfft</span><span class="o">*</span><span class="n">imfft</span><span class="p">)</span><span class="o">.</span><span class="n">real</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">dm</span> <span class="o">=</span> <span class="n">blankim</span>

    <span class="k">if</span> <span class="n">outfits</span><span class="p">:</span>
        <span class="n">hdu</span> <span class="o">=</span> <span class="n">pyfits</span><span class="o">.</span><span class="n">PrimaryHDU</span><span class="p">(</span><span class="n">dm</span><span class="p">,</span><span class="n">header</span><span class="p">)</span>
        <span class="n">hdu</span><span class="o">.</span><span class="n">writeto</span><span class="p">(</span><span class="n">outfits</span><span class="p">,</span><span class="n">clobber</span><span class="o">=</span><span class="n">clobber</span><span class="p">)</span>

    <span class="c">#for xi,yi in zip(x,y):</span>
    <span class="c">#    </span>
    <span class="c">#    blankim += gaussfitter.twodgaussian([1,xi,yi,smoothpix],circle=1,rotate=0,vheight=0)(xax,yax)</span>

    <span class="k">return</span> <span class="n">dm</span></div>
</pre></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>